import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:laoba_menu/views/product-detail/Detail.dart';

class ListItem extends StatefulWidget {
  ListItem(
      {Key key,
      this.name,
      this.cover,
      this.id,
      this.desc,
      this.favorite_count,
      this.create_time})
      : super(key: key);

  final String name;
  final String cover;
  final int id;
  final String desc;
  final int favorite_count;
  final String create_time;
  @override
  ListItemWState createState() => new ListItemWState(this.name, this.cover,
      this.id, this.desc, this.favorite_count, this.create_time);
}

class ListItemWState extends State<ListItem> {
  String name;
  String cover;
  int id;
  String desc;
  int favorite_count;
  String create_time;
  ListItemWState(this.name, this.cover, this.id, this.desc, this.favorite_count,
      this.create_time);

  Dio dio = Dio();
  bool isFavorite = false;
  Color fontColor = Color.fromRGBO(255, 75, 54, 1);
  Color fontUnselectColor = Color.fromRGBO(200, 200, 200, 1);

  _onSelectedIndex() async {
    if (isFavorite) {
      final String url = 'http://127.0.0.1:7001/favorite/cancel';
      final res = await dio.post(url,
          data: {'user_id': 1, 'favorite_type': 1, 'cook_id': this.id});
      isFavorite = false;
      this.favorite_count -= 1;
    } else {
      final String url = 'http://127.0.0.1:7001/cook/setFavoriteCount';
      final res = await dio
          .post(url, data: {'user_id': 1, 'favorite_type': 1, 'id': this.id});
      isFavorite = true;
      this.favorite_count += 1;
    }
    setState(() {});
  }

  void jumpCookDetail(int id, String cover, String name, String desc) {
    Navigator.push(context, MaterialPageRoute(builder: (context) {
      return CookDetail(id: id, cover: cover, name: name, desc: desc);
    }));
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: new BoxDecoration(
        borderRadius: BorderRadius.all(Radius.circular(10.0)),
        color: Colors.white,
      ),
      child: Flex(
        direction: Axis.vertical,
        children: [
          Expanded(
              flex: 1,
              child: GestureDetector(
                  child: Container(
                      padding: const EdgeInsets.fromLTRB(0, 10, 0, 10),
                      decoration: BoxDecoration(
                          borderRadius: BorderRadius.all(Radius.circular(4.0))),
                      child: ClipRRect(
                        borderRadius: BorderRadius.circular(4),
                        child: Image(
                          image: NetworkImage(cover),
                          fit: BoxFit.cover,
                          width: 200,
                        ),
                      )),
                  onTap: () => jumpCookDetail(id, cover, name, desc))),
          Container(
              padding: const EdgeInsets.all(10),
              child: Column(
                children: [
                  Text(
                    "${name}",
                    textAlign: TextAlign.left,
                    maxLines: 2,
                    overflow: TextOverflow.ellipsis,
                    style: TextStyle(fontWeight: FontWeight.bold, fontSize: 16),
                  ),
                  Text(
                    "${desc}",
                    textAlign: TextAlign.left,
                    maxLines: 2,
                    overflow: TextOverflow.ellipsis,
                    style: TextStyle(fontWeight: FontWeight.w400, fontSize: 16),
                  ),
                ],
              )),
          Container(
            padding: const EdgeInsets.fromLTRB(10, 0, 10, 10),
            child: Flex(
              direction: Axis.horizontal,
              children: [
                Expanded(
                  child: Flex(
                    direction: Axis.horizontal,
                    children: [
                      ClipOval(
                        child: Image(
                          image: AssetImage("images/2.jpg"),
                          fit: BoxFit.cover,
                          width: 35,
                          height: 35,
                        ),
                      ),
                      Padding(
                          padding: const EdgeInsets.fromLTRB(5, 0, 0, 0),
                          child: Text(
                            "小飘飘",
                            style: TextStyle(fontWeight: FontWeight.bold),
                          ))
                    ],
                  ),
                  flex: 1,
                ),
                Row(
                  mainAxisSize: MainAxisSize.min,
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    IconButton(
                      icon: Icon(
                        isFavorite == true
                            ? Icons.favorite
                            : Icons.favorite_border,
                        color:
                            isFavorite == true ? fontColor : fontUnselectColor,
                      ),
                      onPressed: () => _onSelectedIndex(),
                    ),
                    Text(
                      "${favorite_count}",
                      style: TextStyle(
                        color: Color.fromRGBO(200, 200, 200, 1),
                      ),
                    )
                  ],
                )
              ],
            ),
          )
        ],
      ),
    );
  }
}
